<template>
  <div class="detail">
    <Header />
    <div class="detail-content">
      <div class="detail-content-top">
        <div style="position: relative;">
          <div class="thing-infos-view">
            <div class="thing-infos">
              <div class="thing-img-box">
                <img :src="detailData.cover" />
              </div>
              <div class="thing-info-box">
                <div class="thing-state">
                  <span class="state hidden-sm">菜品状态</span>
                  <span>上市销售</span>
                </div>
                <h1 class="thing-name">{{ detailData.title }}</h1>
                <span>
                  <span class="a-price-symbol">¥</span>
                  <span class="a-price">{{ detailData.price }}</span>
                </span>
                <button class="buy-btn" @click="handleOrder(detailData)">
                  <img :src="AddIcon" />
                  <span>添加购物车</span>
                </button>
              </div>
            </div>
            <div class="thing-counts hidden-sm">

            </div>
          </div>
        </div>
      </div>
      <div class="detail-content-bottom">
        <div class="thing-content-view flex-view">
          <div class="main-content">
            <div class="order-view main-tab">
          <span class="tab"
                :class="selectTabIndex===index? 'tab-select':''"
                v-for="(item,index) in tabData"
                :key="index"
                @click="selectTab(index)">
            {{ item }}
          </span>
              <span :style="{left: tabUnderLeft + 'px'}" class="tab-underline"></span>
            </div>

            <!--简介-->
            <div class="thing-intro" :class="selectTabIndex <= 0? '':'hide'">
              <p class="text" style="">{{ detailData.description }}</p>
            </div>


          </div>
          <div class="recommend" style="position: relative; top: -290px;">
            <div class="title">种草推荐</div>
            <div class="things">
              <div class="thing-item thing-item" v-for="item in recommendData" @click="handleDetail(item)">
                <div class="img-view">
                  <img :src="item.cover"></div>
                <div class="info-view">
                  <h3 class="thing-name">{{ item.title.substring(0, 12) }}</h3>
                  <span>
                    <span class="a-price-symbol">¥</span>
                    <span class="a-price">{{ item.price }}</span>
                  </span>
                </div>
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>

    <Footer />
  </div>
</template>
<script setup>
import { message } from "ant-design-vue";
import Header from "/@/views/index/components/header.vue";
import Footer from "/@/views/index/components/footer.vue";
import AddIcon from "/@/assets/images/add.svg";
import WantIcon from "/@/assets/images/want-read-hover.svg";
import RecommendIcon from "/@/assets/images/recommend-hover.svg";
import ShareIcon from "/@/assets/images/share-icon.svg";
import WeiboShareIcon from "/@/assets/images/wb-share.svg";
import AvatarIcon from "/@/assets/images/avatar.jpg";
import {
  detailApi,
  listApi as listThingList
} from "/@/api/thing";
import { listThingCommentsApi, createApi as createCommentApi, likeApi } from "/@/api/comment";
import { wishApi } from "/@/api/thingWish";
import { collectApi } from "/@/api/thingCollect";
import { BASE_URL } from "/@/store/constants";
import { useRoute, useRouter } from "vue-router/dist/vue-router";
import { useUserStore } from "/@/store";
import { getFormatTime } from "/@/utils";

const router = useRouter();
const route = useRoute();
const userStore = useUserStore();


let thingId = ref("");
let detailData = ref({});
let tabUnderLeft = ref(6);
let tabData = ref(["简介"]);
let selectTabIndex = ref(0);

let commentData = ref([]);
let recommendData = ref([]);
let sortIndex = ref(0);
let order = ref("recent"); // 默认排序最新

let commentRef = ref();

onMounted(() => {
  thingId.value = route.query.id.trim();
  getThingDetail();
  getRecommendThing();
  getCommentList();
});

const selectTab = (index) => {
  selectTabIndex.value = index;
  tabUnderLeft.value = 6 + 54 * index;
};

const getThingDetail = () => {
  detailApi({ id: thingId.value }).then(res => {
    detailData.value = res.data;
    detailData.value.cover = BASE_URL + "/api/staticfiles/image/" + detailData.value.cover;
  }).catch(err => {
    message.error("获取详情失败");
  });
};
const addToWish = () => {
  let userId = userStore.user_id;
  if (userId) {
    wishApi({ thingId: thingId.value, userId: userId }).then(res => {
      message.success(res.msg);
      getThingDetail();
    }).catch(err => {
      console.log("操作失败");
    });
  } else {
    message.warn("请先登录");
  }
};
const collect = () => {
  let userId = userStore.user_id;
  if (userId) {
    collectApi({ thingId: thingId.value, userId: userId }).then(res => {
      message.success(res.msg);
      getThingDetail();
    }).catch(err => {
      console.log("收藏失败");
    });
  } else {
    message.warn("请先登录");
  }
};
const share = () => {
  let content = "分享一个非常好玩的网站 " + window.location.href;
  let shareHref = "http://service.weibo.com/share/share.php?title=" + content;
  window.open(shareHref);
};
const handleOrder = (detailData) => {
  console.log(detailData);
  const userId = userStore.user_id;
  let gwcDataText = localStorage.getItem("gwc");

  console.log("恢复数据===>" + gwcDataText);
  let gwcList = [];
  if (gwcDataText) {
    let obj = JSON.parse(gwcDataText);
    if (obj && obj.gwc) {
      gwcList = obj.gwc;
    }
    let has = false;
    gwcList.forEach(item => {
      if (item.id === detailData.id) {
        item.count += 1;
        has = true;
      }
    });
    console.log(gwcList);
    if (!has) {
      gwcList.push(
        {
          id: detailData.id,
          title: detailData.title,
          price: detailData.price,
          count: 1
        });
    }
  } else {
    gwcList.push(
      {
        id: detailData.id,
        title: detailData.title,
        price: detailData.price,
        count: 1
      });
  }
  let obj = {
    gwc: gwcList
  };
  let jsonText = JSON.stringify(obj);
  console.log("保存数据===>" + jsonText);
  // 保存购物车
  localStorage.setItem("gwc", jsonText);

  message.success("已添加到购物车");
};
const getRecommendThing = () => {
  listThingList({ sort: "recommend" }).then(res => {
    res.data.forEach((item, index) => {
      if (item.cover) {
        item.cover = BASE_URL + "/api/staticfiles/image/" + item.cover;
      }
    });
    console.log(res);
    recommendData.value = res.data.slice(0, 6);
  }).catch(err => {
    console.log(err);
  });
};
const handleDetail = (item) => {
  // 跳转新页面
  let text = router.resolve({ name: "detail", query: { id: item.id } });
  window.open(text.href, "_blank");
};
const sendComment = () => {
  console.log(commentRef.value);
  let text = commentRef.value.value.trim();
  console.log(text);
  if (text.length <= 0) {
    return;
  }
  commentRef.value.value = "";
  let userId = userStore.user_id;
  if (userId) {
    createCommentApi({ content: text, thingId: thingId.value, userId: userId }).then(res => {
      getCommentList();
    }).catch(err => {
      console.log(err);
    });
  } else {
    message.warn("请先登录！");
    router.push({ name: "login" });
  }
};
const like = (commentId) => {
  likeApi({ id: commentId }).then(res => {
    getCommentList();
  }).catch(err => {
    console.log(err);
  });
};
const getCommentList = () => {
  listThingCommentsApi({ thingId: thingId.value, order: order.value }).then(res => {
    console.log("评论数据:", res.data); // 调试输出

    res.data.forEach(item => {
      item.commentTime = getFormatTime(item.commentTime, true);
    });
    commentData.value = res.data;
  }).catch(err => {
    console.log(err);
  });
};
const sortCommentList = (sortType) => {
  if (sortType === "recent") {
    sortIndex.value = 0;
  } else {
    sortIndex.value = 1;
  }
  order.value = sortType;
  getCommentList();
};

</script>
<style scoped lang="less">

.hide {
  display: none;
}

.detail-content {
  display: flex;
  flex-direction: column;
  width: 1100px;
  margin: 4px auto;
}

.flex-view {
  display: -webkit-box;
  display: -ms-flexbox;
  display: flex;
}

.hidden-lg {
  display: none !important;
}

.thing-infos-view {
  display: flex;
  margin: 89px 0 40px;
  overflow: hidden;

  .thing-infos {
    -webkit-box-flex: 1;
    -ms-flex: 1;
    flex: 1;
    display: flex;
  }

  .mobile-share-box {
    height: 38px;
    background: transparent;
    padding: 0 16px;
    margin: 12px 0;
    font-size: 0;
    -webkit-box-align: center;
    -ms-flex-align: center;
    align-items: center;
    -webkit-box-pack: justify;
    -ms-flex-pack: justify;
    justify-content: space-between;

    .state {
      width: 64px;
      height: 24px;
      line-height: 24px;
      background: rgba(70, 132, 226, .1);
      border-radius: 2px;
      font-weight: 500;
      font-size: 12px;
      color: #4684e2;
      text-align: center;
    }

    .share-img {
      background: #fff;
      width: 38px;
      height: 38px;
      border-radius: 50%;
      text-align: center;

      img {
        position: relative;
        top: 4px;
        width: 24px;
      }
    }
  }

  .thing-img-box {
    -webkit-box-flex: 0;
    -ms-flex: 0 0 235px;
    flex: 0 0 235px;
    margin: 0 40px 0 0;

    img {
      width: 255px;
      height: 200px;
      display: block;
      background-size: cover;
      object-fit: cover;
    }
  }

  .thing-info-box {
    text-align: left;
    padding: 0;
    margin: 0;
  }

  .thing-state {
    height: 26px;
    line-height: 26px;

    .state {
      font-weight: 500;
      color: #4684e2;
      background: rgba(70, 132, 226, .1);
      border-radius: 2px;
      padding: 5px 8px;
      margin-right: 16px;
    }

    span {
      font-size: 14px;
      color: #152844;
    }
  }

  .thing-name {
    line-height: 32px;
    margin: 16px 0;
    color: #0F1111 !important;
    font-size: 18px !important;
    font-weight: 400 !important;
    font-style: normal !important;
    text-transform: none !important;
    text-decoration: none !important;
  }

  .translators, .authors {
    line-height: 18px;
    font-size: 14px;
    margin: 8px 0;
    -webkit-box-align: start;
    -ms-flex-align: start;
    align-items: flex-start;
    -webkit-box-pack: start;
    -ms-flex-pack: start;
    justify-content: flex-start;

    .name {
      color: #315c9e;
      white-space: normal;
    }
  }

  .tags {
    position: absolute;
    bottom: 20px;
    margin-top: 16px;

    .category-box {
      color: #152844;
      font-size: 14px;

      .title {
        color: #787878;
      }
    }
  }

  .thing-counts {
    -webkit-box-flex: 0;
    -ms-flex: 0 0 235px;
    flex: 0 0 235px;
    margin-left: 20px;
  }

  .pointer {
    cursor: pointer;
  }

  .count-item {
    height: 64px;
    -webkit-box-align: center;
    -ms-flex-align: center;
    align-items: center;
    cursor: pointer;
  }

  .count-img {
    -webkit-box-flex: 0;
    -ms-flex: 0 0 32px;
    flex: 0 0 32px;
    margin-right: 24px;
    font-size: 0;

    img {
      width: 100%;
      display: block;
    }
  }

  .count-box {
    position: relative;
    border-bottom: 1px solid #cedce4;
    -webkit-box-align: center;
    -ms-flex-align: center;
    align-items: center;
    -webkit-box-pack: justify;
    -ms-flex-pack: justify;
    justify-content: space-between;
    -webkit-box-flex: 1;
    -ms-flex: 1;
    flex: 1;
    height: 100%;
  }

  .count-text-box {
    font-size: 0;

    .count-title {
      color: #152844;
      font-weight: 600;
      font-size: 16px;
      line-height: 18px;
      display: block;
      height: 18px;
    }
  }

  .count-num-box {
    font-weight: 600;
    font-size: 20px;
    line-height: 24px;
    color: #152844;
  }
}

.buy-btn {
  cursor: pointer;
  display: block;
  background: #4684e2;
  border-radius: 4px;
  text-align: center;
  color: #fff;
  font-size: 14px;
  height: 36px;
  line-height: 36px;
  width: 110px;
  outline: none;
  border: none;
  margin-top: 18px;
}

.buy-btn img {
  width: 12px;
  margin-right: 2px;
  vertical-align: middle;
}

.buy-btn span {
  vertical-align: middle;
}

.buy-way {
  overflow: hidden;

  .title {
    font-weight: 600;
    font-size: 18px;
    height: 26px;
    line-height: 26px;
    color: #152844;
    margin-bottom: 12px;
  }
}

.thing-content-view {
  margin-top: 40px;
  padding-bottom: 50px;
}

.main-content {
  -webkit-box-flex: 1;
  -ms-flex: 1;
  flex: 1;

  .text {
    color: #484848;
    font-size: 16px;
    line-height: 26px;
    padding-left: 12px;
    margin: 11px 0;
    white-space: pre-wrap;
  }
}

.main-tab {
  border-bottom: 1px solid #cedce4;
}

.order-view {
  position: relative;
  color: #6c6c6c;
  font-size: 14px;
  line-height: 40px;

  .title {
    margin-right: 8px;
  }

  .tab {
    margin-right: 20px;
    cursor: pointer;
    color: #5f77a6;
    font-size: 16px;
    cursor: pointer;
  }

  .tab-select {
    color: #152844;
    font-weight: 600;
  }

  .tab-underline {
    position: absolute;
    bottom: 0;
    left: 84px;
    width: 16px;
    height: 4px;
    background: #4684e2;
    -webkit-transition: left .3s;
    transition: left .3s;
  }
}

.recommend {
  -webkit-box-flex: 0;
  -ms-flex: 0 0 235px;
  flex: 0 0 235px;
  margin-left: 20px;

  .title {
    font-weight: 600;
    font-size: 18px;
    line-height: 26px;
    color: #152844;
    margin-bottom: 12px;
  }

  .things {
    border-top: 1px solid #cedce4;

    .thing-item {
      min-width: 255px;
      max-width: 255px;
      position: relative;
      flex: 1;
      margin-right: 20px;
      height: fit-content;
      overflow: hidden;
      margin-top: 26px;
      margin-bottom: 36px;
      padding-bottom: 24px;
      border-bottom: 1px #e1e1e1 solid;
      cursor: pointer;

      .img-view {
        //background: #f3f3f3;
        //text-align: center;
        height: 200px;
        width: 255px;
        //border: 1px #f3f3f3 solid;

        img {
          height: 200px;
          width: 255px;
          overflow: hidden;
          margin: 0 auto;
          background-size: cover;
          object-fit: cover;
        }
      }

      .info-view {
        //background: #f6f9fb;
        overflow: hidden;
        padding: 0 16px;

        .thing-name {
          line-height: 32px;
          margin-top: 12px;
          color: #0F1111 !important;
          font-size: 15px !important;
          font-weight: 400 !important;
          font-style: normal !important;
          text-transform: none !important;
          text-decoration: none !important;
        }

        .price {
          color: #ff7b31;
          font-size: 20px;
          line-height: 20px;
          margin-top: 4px;
          overflow: hidden;
          text-overflow: ellipsis;
          white-space: nowrap;
        }

        .translators {
          color: #6f6f6f;
          font-size: 12px;
          line-height: 14px;
          margin-top: 4px;
          overflow: hidden;
          text-overflow: ellipsis;
          white-space: nowrap;
        }
      }
    }

  }
}

.flex-view {
  display: flex;
}

.thing-comment {
  .title {
    font-weight: 600;
    font-size: 14px;
    line-height: 22px;
    height: 22px;
    color: #152844;
    margin: 24px 0 12px;
  }

  .publish {
    -webkit-box-align: center;
    -ms-flex-align: center;
    align-items: center;

    .mine-img {
      -webkit-box-flex: 0;
      -ms-flex: 0 0 40px;
      flex: 0 0 40px;
      margin-right: 12px;
      border-radius: 50%;
      width: 40px;
      height: 40px;
    }

    .content-input {
      -webkit-box-flex: 1;
      -ms-flex: 1;
      flex: 1;
      background: #f6f9fb;
      border-radius: 4px;
      height: 32px;
      line-height: 32px;
      color: #484848;
      padding: 5px 12px;
      white-space: nowrap;
      outline: none;
      border: 0px;
    }

    .send-btn {
      margin-left: 10px;
      background: #4684e2;
      border-radius: 4px;
      -webkit-box-flex: 0;
      -ms-flex: 0 0 80px;
      flex: 0 0 80px;
      color: #fff;
      font-size: 14px;
      text-align: center;
      height: 32px;
      line-height: 32px;
      outline: none;
      border: 0px;
      cursor: pointer;
    }
  }

  .tab-view {
    -webkit-box-pack: justify;
    -ms-flex-pack: justify;
    justify-content: space-between;
    font-size: 14px;
    -webkit-box-align: center;
    -ms-flex-align: center;
    align-items: center;
    margin: 24px 0;

    .count-text {
      color: #484848;
      float: left;
    }

    .tab-box {
      color: #5f77a6;
      -webkit-box-align: center;
      -ms-flex-align: center;
      align-items: center;

      .tab-select {
        color: #152844;
      }

      span {
        cursor: pointer;
      }
    }

    .line {
      width: 1px;
      height: 12px;
      margin: 0 12px;
      background: #cedce4;
    }
  }
}


.comments-list {
  .comment-item {
    .flex-item {
      -webkit-box-align: center;
      -ms-flex-align: center;
      align-items: center;
      padding-top: 16px;

      .avator {
        -webkit-box-flex: 0;
        -ms-flex: 0 0 40px;
        flex: 0 0 40px;
        width: 40px;
        height: 40px;
        margin-right: 12px;
        border-radius: 50%;
        cursor: pointer;
      }

      .person {
        -webkit-box-flex: 1;
        -ms-flex: 1;
        flex: 1;
      }

      .name {
        color: #152844;
        font-weight: 600;
        font-size: 14px;
        line-height: 22px;
        height: 22px;
        cursor: pointer;
      }

      .time {
        color: #5f77a6;
        font-size: 12px;
        line-height: 16px;
        height: 16px;
        margin-top: 2px;
      }

      .float-right {
        color: #4684e2;
        font-size: 14px;
        float: right;

        span {
          margin-left: 19px;
          cursor: pointer;
        }

        .num {
          color: #152844;
          margin-left: 6px;
          cursor: auto;
        }
      }
    }
  }
}

.comment-content {
  margin-top: 8px;
  color: #484848;
  font-size: 14px;
  line-height: 22px;
  padding-bottom: 16px;
  border-bottom: 1px dashed #cedce4;
  margin-left: 52px;
  overflow: hidden;
  word-break: break-word;
}

.infinite-loading-container {
  clear: both;
  text-align: center;
}

.a-price-symbol {
  top: -0.5em;
  font-size: 12px;
}

.a-price {
  color: #0F1111;
  font-size: 21px;
}
</style>
